package com.linln.admin.pcs.controller;


import com.linln.admin.pcs.service.VerifyLogService;
import com.linln.admin.system.service.AppService;
import com.linln.common.data.QuerySpec;
import com.linln.common.utils.HttpServletUtil;
import com.linln.common.utils.ResultVoUtil;
import com.linln.common.utils.ValidationUtil;
import com.linln.component.actionLog.annotation.EntityParam;
import com.linln.component.shiro.ShiroUtil;
import com.linln.modules.system.domain.ContractTpl;
import com.linln.modules.system.domain.Dep;
import com.linln.modules.system.domain.User;
import com.linln.modules.system.repository.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.thymeleaf.context.Context;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;

import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 科室管理
 */
@Controller
@RequestMapping("/pcs/dep")
public class DepController {

    @Autowired
    private AppService appService;
    @Autowired
    private  ProjectRepository projectRepository;
    @Autowired
    private CompanyRepository companyRepository;
    @Autowired
    private PorterRepository porterRepository;
    @Autowired
    private ContractTplRepository contractTplRepository;

    @Autowired
    private VerifyLogService verifyLogService;
    @Autowired
    private ThymeleafViewResolver thymeleafViewResolver;
    @Autowired
    private DepRepository depRepository;

    /**
     * 合同模板页面
     */
    @GetMapping("/index")
    @RequiresPermissions("pcs:dep:index")
    public ResponseEntity<?> index(Model model, @EntityParam Dep dep) {
        return appService.layTable(depRepository, before -> {
                    // 添加模板变量
                    return model;

                },

                after -> {
                    QuerySpec querySpec = QuerySpec.matching();
                    querySpec.withMatcher("name", QuerySpec.LIKE);

                    return QuerySpec.of(querySpec);
                },

                "/pcs/dep/index",

                (pageResult) -> {
                    // 在这里处理 pageResult
//                    pageResult.getContent().forEach(item -> {
//                        ContractTpl data = (ContractTpl) item;
//                        QuerySpec querySpec = QuerySpec.matching();
//                        querySpec.withMatcherValue("companyId", QuerySpec.EQUAL, String.valueOf(data.getId()))
//                                .withMatcherValue("status", QuerySpec.EQUAL, String.valueOf(1));
//                        Specification<Porter> specification = QuerySpec.of(querySpec);
//                        data.setPorterNum(porterRepository.count(specification));
//                        item = data;
//                    });
                }

        );

    }


    /**
     * 表单
     */
    @RequestMapping("/form")
    @RequiresPermissions("pcs:dep:index")
    public ResponseEntity<?> form(Model model, @EntityParam Dep dep) throws Exception {

        return appService.form(depRepository, "/pcs/dep/form",
                (isEdit) -> {

                    return model;

                },
                (isEdit, vo) -> {

                    model.addAttribute("vo", vo);
                    // 你可以在这里添加其他逻辑
                    return model;
                },
                (data) -> {
                    // 保存前数据校验
                    List<ValidationUtil.ValidationRule<?>> rules = new ArrayList<>();
                    // 添加邮箱的校验规则
                    //            rules.add(new ValidationUtil.ValidationRule<>(
                    //                    porter.getCardNo(),
                    //                    email -> email != null ,
                    //                    "Email格式不正确"
                    //            ));

                    // 执行校验
                    return ValidationUtil.validateFields(rules);

                },
                (data) -> {

                    dep.setStatus((byte) 1);
                    return dep;
                },

                (data) -> {
                    // 保存后续逻辑在这写
                });

    }



    /**
     * 修改状态
     * @param model
     * @param dep
     * @return
     * @throws Exception
     */
    @RequestMapping("/state")
    @RequiresPermissions("pcs:dep:index")
    public ResponseEntity<?> state(Model model, @EntityParam Dep dep) throws Exception {

        return appService.save(depRepository, (data) -> {

            // 校验数据
            return ValidationUtil.validateFields(new ArrayList<>());

        }, (data) -> {
            // 处理数据
            Dep origin = depRepository.getById(dep.getId());
            origin.setStatus(dep.getStatus());

            return origin;

        }, (data) -> {
            // 更新后操作
        });

    }









}
